<template>
  <div class="result-container">
    <div class="title-wrap">
      <h2 class="title">{{$route.query.name}}</h2>
      <span class="sub-title">找到{{count}}个结果</span>
    </div>
    <el-tabs v-model="activeIndex">
      <el-tab-pane label="歌曲" name="songs">
        <table class="el-table">
          <thead>
            <th></th>
            <th>音乐标题</th>
            <th>歌手</th>
            <th>专辑</th>
            <th>时长</th>
          </thead>
          <tbody>
            <tr class="el-table__row" v-for="(list, index) in lists" :key="index" @dblclick="play(list.id)">
              <td>{{index+1}}</td>
              <td>
                <div class="song-wrap">
                  <div class="name-wrap">
                    <span>{{list.name}}</span>
                    <span v-if="list.mvid" class="iconfont icon-mv"></span>
                  </div>
                  <span v-if="list.alias.length">{{list.alias[0]}}</span>
                </div>
              </td>
              <td>{{list.artists[0].name}}</td>
              <td>{{list.album.name}}</td>
              <td>{{list.duration | date-format}}</td>
            </tr>

          </tbody>
        </table>
      </el-tab-pane>
      <el-tab-pane label="歌单" name="lists">
        <div class="items">
          <div v-for="(playlist, index) in playlists" :key="index" class="item" @click="toPlaylist(playlist.id)">
            <div class="img-wrap">
              <div class="num-wrap">
                播放量:
                <span class="num">{{playlist.playCount}}</span>
              </div>
              <img :src="playlist.coverImgUrl" alt="" />
              <span class="iconfont icon-play"></span>
            </div>
            <p class="name">{{playlist.name}}</p>
          </div>
        </div>
      </el-tab-pane>
      <el-tab-pane label="MV" name="mv">
        <div class="items mv">
          <div class="item" v-for="(mvlist, index) in mvlists" :key="index" @click="toMV(mvlist.id)">
            <div class="img-wrap">
              <img :src="mvlist.cover" alt="" />
              <span class="iconfont icon-play"></span>
              <div class="num-wrap">
                <div class="iconfont icon-play"></div>
                <div class="num">{{mvlist.playCount}}</div>
              </div>
              <span class="time">{{mvlist.duration | date-format}}</span>
            </div>
            <div class="info-wrap">
              <div class="name">{{mvlist.name}}</div>
              <div class="singer">{{mvlist.artistName}}</div>
            </div>
          </div>
        </div>
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<script>
import axios from 'axios'
export default {
  name: 'result',
  data() {
    return {
      // tab切换时会改变的数据
      activeIndex: 'songs',
      lists:[],
      // 搜索结果的总数
      count:0,
      // 保存歌单字段
      playlists:[],
      //保存mv字段
      mvlists:[]
    };
  },
  methods: {
    play(id){
      axios({
        url:'https://autumnfish.cn/song/url',
        method:'get',
        params:{
          id
        }
      }).then(res=>{
        console.log(res);
        let url = res.data.data[0].url
        this.$parent.musicUrl = url
      })
    },
    // 跳转到歌单详情页
    toPlaylist(id){
      this.$router.push(`/playlist?name=${id}`)
    },
    // 跳转到Mv界面
    toMV(id){
      this.$router.push(`/mv?name=${id}`)
    }
  },
  created() {
    axios({
      url:'https://autumnfish.cn/search',
      method:'get',
      params:{
        keywords:this.$route.query.name,
        type:1
      }
    }).then(res=>{
      // console.log(res);
      this.lists = res.data.result.songs
      this.count = res.data.result.songCount
    })
  },
  watch:{
    activeIndex(){
      // songs歌曲
      // lists歌单
      // mv   MV
      let type = 1;
      let limit = 10;
      switch (this.activeIndex) {
        case 'songs':
          type = 1;
          limit = 10;
          break;
        case 'lists':
          type = 1000;
          limit = 10;
          break;
        case 'mv':
        type = 1004;
        limit = 12;
          break;
        default:
          break;
      }
    axios({
      url:'https://autumnfish.cn/search',
      method:'get',
      params:{
        keywords:this.$route.query.name,
        type,
        limit
      }
    }).then(res=>{
      console.log(res);
      if(type==1){
        // 歌曲
        this.lists = res.data.result.songs
        this.count = res.data.result.songCount
      }else if(type==1000){
        // 歌单
        this.playlists = res.data.result.playlists
        // 总数
        this.count = res.data.result.playlistCount
        // 处理播放次数
        for(let i = 0;i<this.playlists.length;i++){
          if(this.playlists[i].playCount>10000){
            this.playlists[i].playCount =parseInt(Math.floor( this.playlists[i].playCount/10000))+'万'
          }
        }
      }else{
        // mv
        this.mvlists = res.data.result.mvs
        this.count = res.data.result.mvCount
        // 处理播放次数
        for(let i = 0;i<this.mvlists.length;i++){
          if(this.mvlists[i].playCount>10000){
            this.mvlists[i].playCount =parseInt(Math.floor( this.mvlists[i].playCount/10000))+'万'
          }
        }
      }
    })
    }
  }
};
</script>

<style >

</style>
